package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.main.base.adapter.ImageAddAdapter;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;

import java.util.List;

/**
 * Created by fangzhou on 2017/1/14.
 * 地图导航dialog
 */

public class ReturnGoodsDialog extends Dialog implements ImageAddAdapter.ImageViewAddListener,
        ImageAddAdapter.ImageViewClearListener {
    private Context context;
    private ImageAddAdapter imageAddAdapter;
    private OnDialogClickListener sureClickListener;

    public ReturnGoodsDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
    }

    public ReturnGoodsDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public ReturnGoodsDialog setOnSureClickListener(OnDialogClickListener sureClickListener) {
        this.sureClickListener = sureClickListener;
        return this;
    }

    @Override
    public void imgClear(View view, int position) {

    }

    @Override
    public void imgAdd(View view) {

    }

    public ReturnGoodsDialog builder() {
        init();
        return this;
    }

    private void init() { //规格
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_return_goods, null);
        EditText proDescribe = dialogView.findViewById(R.id.proDescribe);
        TextView tvSubmit = dialogView.findViewById(R.id.tv_submit);
        ImageView imageDismiss = dialogView.findViewById(R.id.image_dismiss);
        RecyclerView imageRecyclerView = dialogView.findViewById(R.id.image_RecyclerView);
        imageDismiss.setOnClickListener(v -> {
            dismiss();
        });
        tvSubmit.setOnClickListener(v -> {
//            if (StringUtil.isEmpty(proDescribe.getText().toString())) {
//                ToastUtils.showShort("请输入退货原因");
//                return;
//            }
            sureClickListener.onDialogClick(proDescribe.getText().toString(), imageAddAdapter.getList());

        });
        imageAddAdapter = new ImageAddAdapter(context);
        imageAddAdapter.setImageViewAddListener(this);
        imageAddAdapter.setImageViewClearListener(this);
        imageRecyclerView.setLayoutManager(new FullyGridLayoutManager(context, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        imageRecyclerView.setAdapter(imageAddAdapter);

        setContentView(dialogView);
        Window dialogWindow = getWindow();
        if (dialogWindow == null) {
            return;
        }
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(dm);
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    dm.heightPixels * 2 / 3);
        } else {
            dialogWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    public interface OnDialogClickListener {
        void onDialogClick(String describe, List<String> imgList);
    }

}
